package com.gingko.duominuo.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.gingko.duominuo.R
import com.gingko.duominuo.databinding.FragmentReviewConditionsBinding
import com.gingko.duominuo.ui.adapter.ReviewConditionsAdapter
import com.gingko.duominuo.ui.widget.DefaultItemDecoration
import com.gingko.duominuo.utils.dp2px
import com.gingko.duominuo.utils.singleClick
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class ReviewConditionsFragment : BottomSheetDialogFragment() {
    private val mViewBinding: FragmentReviewConditionsBinding by lazy { FragmentReviewConditionsBinding.inflate(layoutInflater) }
    private val conditions by lazy { mutableListOf<String>() }
    private val conditionsAdapter by lazy { ReviewConditionsAdapter(requireContext(), conditions) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
        arguments?.let {
            conditions.addAll(it.getStringArrayList(ARG_CONDITIONS) ?: listOf())
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewBinding.ivClose.singleClick {
            dismissAllowingStateLoss()
        }
        val divider = DefaultItemDecoration(requireContext(), LinearLayoutManager.VERTICAL)
        divider.setDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.shape_review_conditions_divider)!!)
        mViewBinding.rcvReviewConditions.addItemDecoration(divider)
        mViewBinding.rcvReviewConditions.adapter = conditionsAdapter

    }

    companion object {
        private const val ARG_CONDITIONS = "conditions"

        @JvmStatic
        fun newInstance(conditions: List<String>) =
            ReviewConditionsFragment().apply {
                arguments = Bundle().apply {
                    putStringArrayList(ARG_CONDITIONS, conditions as ArrayList<String>)
                }
            }
    }
}